/**
 * 
 */
package com.bagtrack.service.impl;

import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;

import com.bagtrack.dao.IFlightDAO;
import com.bagtrack.dao.IJobDAO;
import com.bagtrack.model.Flight;
import com.bagtrack.model.Gate;
import com.bagtrack.model.Job;
import com.bagtrack.model.Route;
import com.bagtrack.model.SearchQuery;
import com.bagtrack.model.StartJobRequest;
import com.bagtrack.model.User;
import com.bagtrack.service.IJobService;


/**
 *
 * @author Brian Boyle
 * Nov 16, 2011
 */
public class JobServiceImpl implements IJobService {

    @Autowired
    private IJobDAO jobDAO;
    @Autowired
    private IFlightDAO flightDAO;

    public void setJobDAO(IJobDAO jobDAO) {
        this.jobDAO = jobDAO;
    }

    public void setFlightDAO(IFlightDAO flightDAO) {
        this.flightDAO = flightDAO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Job startJob(StartJobRequest startJobRequest) {

        User user = new User();
        user.setId(startJobRequest.getUserId());
        Flight flight = flightDAO.selectFlightById(startJobRequest.getFlightId());

        Job job = new Job(Job.UNLOADING, startJobRequest.getLongitude(), startJobRequest.getLatitude(), 0.0, new Date(), new Date());

        job.setFlight(flight);
        job.setUser(user);
        Gate gate = flight.getGate();
        Route route = gate.getRoute();
        Route r = new Route();
        r.setId(route.getId());
        job.setRoute(r);
        jobDAO.saveJob(job);
        return job;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public HashMap<String, String> searchJobs(SearchQuery query) {
        return jobDAO.searchJobs(query);
    }
}
